const config = require("../../../../../utils/config.js");
var QQMapWX = require("../../../../../utils/qqmap.js");
var qqMap = new QQMapWX({
  key: config.key,
});
import api from "../../../../../common/api.js";
import * as transferData from "../../../../../models/transferData";
import locationBehavior from "../../../../../behaviors/location.js";


const app = getApp();
Page({
  behaviors: [locationBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    choosefloor: -1,
    rows: [],
    showCreat: false,
    keyWords: "",
    subscribe: true,
  },
  //取消
  goRrevPage() {
    wx.navigateBack({
      detal: 0,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.choosefloor) {
      this.setData({
        choosefloor: options.choosefloor,
      });
    }
    if (options.mainBuilding) {
      this.setData({
        mainBuilding: options.mainBuilding,
      });
    }
  },

  //选择城市
  goSwitchCity() {
    wx.navigateTo({
      url: "/pages/home/switchCity/switchCity",
    });
  },
  //search keyWords
  bindKeyInput(data) {
    var keyWords = data ? data.detail.value : this.data.keyWords;
    var that = this;
    keyWords = keyWords && keyWords.trim();
    if (!keyWords) {
      return that.setData({
        rows: [],
        keyWords: "",
      });
    }

    api.villageList(
      {
        resblockName: keyWords,
        pageNo: 1,
        rows: 10,
        city: that.data.currentCity && that.data.currentCity.city,
        isExternal: that.data.subscribe ? true : null,
      },
      (res) => {
        if (!res.data.isSuccess) return;

        let rowId = [];
        let subscribeRow = res.data.content.rows;
        subscribeRow.map((v) => rowId.push(v.hdResblockId));

        api.houseOwnerSubscribeList(rowId, (rev) => {
          let contentId = rev.data.content;
          subscribeRow.map((n) => {
            let isSubscribe = contentId.find((o) => {
              return o == n.hdResblockId;
            });
            if (isSubscribe) {
              n.subscribe = true;
            } else {
              n.subscribe = false;
            }
            that.setData({
              keyWords: keyWords,
              rows: subscribeRow,
              records: res.data.content.records,
              loading: false,
              showCreat: true,
            });
          });
        });

        if (res.data.content.records < 10) {
          that.mapSearchList();
        }
      }
    );
  },
  mapSearchList() {
    var that = this;
    qqMap.getSuggestion({
      keyword: that.data.keyWords,
      region: that.data.currentCity && that.data.currentCity.city,
      region_fix: 1,
      success: function (res) {
        that.setData({
          mapSearchList: res.data,
        });
      },
      fail: function (res) {},
    });
  },

  getSubscribe(e) {
    let sub = e.currentTarget.dataset.sub;
    let hdResblockId = e.currentTarget.dataset.hdresblockid;
    if (sub === '0') {
      wx.showLoading({
        title: '取消订阅...',
      })
    } else {
      wx.showLoading({
        title: '订阅中...',
      })
    }
    api.doSubscribeResblock({
      hdResblockId,
      isSubscribe: sub,
    }, res => {
      wx.hideLoading()
      const {rows} = this.data
      transferData.setData('isRefreshMySubscribe', true)
      if(res.data.errorCode==='31001'){ //无该小区
        wx.showModal({
          title: '温馨提示',
          content: '该小区暂不支持订阅',
          confirmText: '联系客服',
          cancelText: '关闭',
          confirmColor: '#F75352',
          success(res) {
            if (res.confirm) {
              api.getPrivateSale({}, (res1) => {
                wx.makePhoneCall({
                  phoneNumber: res1.data.content.serviceMobile,
                  success() {},
                  fail() {},
                });
              });
            }
          },
        })
      } else {
        rows.forEach(item => {
          if(item.hdResblockId === hdResblockId) {
            item.subscribe = sub == 1 ? true : false
          }
        })
      }
      this.setData({
        rows
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
   onShow: function() {
    this.getCityInfoNew().then((res) => {
      this.setData({
        currentCity: res,
      });
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},
});
